/*--------------------------------------------------------------------------------------------------------------------
(C) Copyright 2006, 2007 Marvell DSPC Ltd. All Rights Reserved.
-------------------------------------------------------------------------------------------------------------------*/

/*
 * serial_client.c
 *
 * Use to test AT Command Server 
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h> /* close */
#include <pthread.h>
#include <termios.h>
#include <fcntl.h>
#include <stdlib.h>


#define DATABITS       CS8
//#define BAUD        B38400
#define BAUD        	B115200
#define STOPBITS        0
#define PARITYON        0
#define PARITY          0
int openchannel (char * pchannelname) {
        int serialfd;
	struct termios oldtio, newtio;       //place for old and new port settings for serial port

	/*
	 *  Open the Device
	 */

	/*open the device(com port) to be non-blocking (read will return immediately) */
	serialfd = open(pchannelname, O_RDWR   ); //| O_NOCTTY

	if (serialfd <= 0){
		printf("Error opening device %s\n", pchannelname);
		return 0;
	}


	fcntl(serialfd, F_SETFL, 0);

	tcgetattr(serialfd,&oldtio); // save current port settings

	// set new port settings for canonical input processing
	newtio.c_cflag = BAUD | CRTSCTS | DATABITS | STOPBITS | PARITYON | PARITY | CLOCAL | CREAD;
	newtio.c_iflag = IGNPAR;
	newtio.c_oflag = 0;
	///newtio.c_lflag =  ECHOE | ECHO | ICANON;       //ICANON;
	newtio.c_lflag =  0;       //ICANON;

	newtio.c_cc[VMIN]=1;
	newtio.c_cc[VTIME]=0;
	newtio.c_cc[VERASE] = 0x8;
	newtio.c_cc[VEOL] = 0xD;

	newtio.c_cc[VINTR]    = 0;     /* Ctrl-c */
	newtio.c_cc[VQUIT]    = 0;     /* Ctrl-\ */
	newtio.c_cc[VERASE]   = 0;     /* del */
	newtio.c_cc[VKILL]    = 0;     /* @ */
	newtio.c_cc[VEOF]     = 4;     /* Ctrl-d */
	newtio.c_cc[VTIME]    = 0;     /* inter-character timer unused */
	newtio.c_cc[VMIN]     = 1;     /* blocking read until 1 character arrives */
	newtio.c_cc[VSWTC]    = 0;     /* '\0' */
	newtio.c_cc[VSTART]   = 0;     /* Ctrl-q */
	newtio.c_cc[VSTOP]    = 0;     /* Ctrl-s */
	newtio.c_cc[VSUSP]    = 0;     /* Ctrl-z */
	newtio.c_cc[VEOL]     = 0;     /* '\0' */
	newtio.c_cc[VREPRINT] = 0;     /* Ctrl-r */
	newtio.c_cc[VDISCARD] = 0;     /* Ctrl-u */
	newtio.c_cc[VWERASE]  = 0;     /* Ctrl-w */
	newtio.c_cc[VLNEXT]   = 0;     /* Ctrl-v */
	newtio.c_cc[VEOL2]    = 0;     /* '\0' */

	newtio.c_cc[VMIN]=1;
	newtio.c_cc[VTIME]=0;
	newtio.c_cc[VERASE] = 0x8;
	newtio.c_cc[VEOL] = 0xD;



	tcflush(serialfd, TCIFLUSH);
	tcsetattr(serialfd,TCSANOW,&newtio);

	/* indicate the port is open */
	//write(serialfd, "COMM port ready.\n", 16);
        return serialfd;
}

int ReadChannel(int fd,char * pbuf)
{
     int bytes = 0;
     if ((bytes = read(fd, pbuf, 256)) < 1) {
	 perror("cannot read data ");
    }
    pbuf[bytes] = '\0';        /* Assure null terminated string */
    return bytes;
}
int WriteChannel(int fd,char* pbuf)
{
  char wbuf[256];
  int rc;
  strcpy(wbuf,pbuf);
  int len = strlen(pbuf);
  
  wbuf[len] = 0xd;
  rc = write(fd,wbuf,len + 1);
  if(rc<0) {
	 perror("cannot send data ");

  }
  return rc;
}
